// 全局共享数据示例
import { DEFAULT_NAME, UPLOAD_URL, CERT_UPLOAD_URL, UPLOAD_URL_FILE } from '@/constants';
import { useState, useEffect, useRef } from 'react';
import { getCapture, login, fetchUser } from '@/services/Platform';
import { getAccessToken } from '@/utils/utils';

interface captureParams {
  account: string;
}

interface loginParams {
  username: string;
  password: string;
}

// Entry component
import { App } from 'antd'
import type { MessageInstance } from 'antd/es/message/interface'
import type { ModalStaticFunctions } from 'antd/es/modal/confirm'
import type { NotificationInstance } from 'antd/es/notification/interface'

let message: MessageInstance
let notification: NotificationInstance
let modal: Omit<ModalStaticFunctions, 'warn'>

// export default () => {
//   const staticFunction = App.useApp()
//   message = staticFunction.message
//   modal = staticFunction.modal
//   notification = staticFunction.notification
//   return null
// }

// export { message, notification, modal }


export default () => {
  const [name, setName] = useState<string>(DEFAULT_NAME!);
  const [isLoading, setIsLoading] = useState<boolean>(false);
  const [routerList, setRouterList] = useState<any>();
  const [userInfo, setUserInfo] = useState<any>({});
  const [authList, setAuthList] = useState<any>([])            // 身份列表
  const [contentWidth, setContentWidth] = useState<number>(800);

  const GetCapture = (data: captureParams): any => {
    return getCapture(data);
  };

  const Login = (data: loginParams) => {
    return login(data);
  };

  const CheckUser = () => {
    return new Promise((resolve, reject) => {
      const token = getAccessToken()
      if (token) {
        resolve(true)
      } else {
        reject(false);
      }
    });
  };

  const FetchUser = () => {
    return new Promise((resolve, reject) => {
      fetchUser().then(
        (res) => {
          resolve(res);
        },
        (err) => {
          reject(false);
        },
      );
    });
  };

  // 检查操作权限
  const CheckOptionAuth = (key: string) => {
    return !!authList.find((i: any) => i.action === key)
  }

  return {
    name,
    setName,
    isLoading,
    setIsLoading,
    routerList,
    setRouterList,
    userInfo,
    setUserInfo,
    contentWidth,
    setContentWidth,
    authList,
    setAuthList,


    GetCapture,
    Login,
    CheckUser,
    FetchUser,
    UPLOAD_URL,
    CERT_UPLOAD_URL,
    UPLOAD_URL_FILE,
    CheckOptionAuth,
  };
};
